﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using NetBay.Core.BusinessEntities;
using System.Collections.Generic;
using System.Linq;

namespace Rio.Framework.Pages.Helpers.Utils
{
    public static class Utils
    {
        public static DependencyObject GetParentByBaseType(this DependencyObject depObject, Type t, bool recursive)
        {
            var parent = VisualTreeHelper.GetParent(depObject) as FrameworkElement;
            if (parent != null)
            {
                Type pt = parent.GetType();
                if (pt == t)
                    return parent;

                if (recursive)
                    return GetParentByBaseType(parent, t, true);
            }

            return null;
        }

        /// <summary>
        /// Remove extention without remove root Directory 
        /// </summary>
        /// <param name="filename">File name </param>
        /// <returns></returns>
        public static string RemoveExtension(string filename)
        {
            if (string.IsNullOrEmpty(filename))
                return string.Empty;

            return filename.Substring(0, filename.Length - Path.GetExtension(filename).Length);
        }

        /// <summary>
        /// Gets the folder label.
        /// </summary>
        /// <param name="folder">The folder.</param>
        /// <returns></returns>
        public static string GetFolderLabel(Folder folder)
        {
            string tabLabel = string.Empty;
            try
            {
                Dictionary<int, string> labelRankValue = new Dictionary<int, string>();
                List<string> values = folder.ListValues.ToList();
                List<Index> indexes = folder.ListIndex.ToList();
                for (int i = 0; i < indexes.Count - 1; i++)
                {
                    if (indexes[i].LabelRank != null && !labelRankValue.ContainsKey(indexes[i].LabelRank.Value))
                        labelRankValue.Add(indexes[i].LabelRank.Value, values[i]);
                }
                var sortedList = from q in labelRankValue orderby q.Key ascending select q;
                foreach (var item in sortedList)
                {
                    if (!string.IsNullOrEmpty(item.Value))
                        tabLabel += item.Value + "-";
                }
                tabLabel = tabLabel.TrimEnd('-');
            }
            catch (Exception ex)
            {
                
            }
            return tabLabel;
        }
    }
}
